package com.geccocrawler.gecco.demo.jd; /**
 * Created by ScorpionOrange on 2017/03/19.
 */
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.List;

import net.sf.cglib.beans.BeanMap;

import com.alibaba.fastjson.JSON;
import com.geccocrawler.gecco.annotation.FieldRenderName;
import com.geccocrawler.gecco.downloader.DownloaderContext;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.response.HttpResponse;
import com.geccocrawler.gecco.spider.SpiderBean;
import com.geccocrawler.gecco.spider.render.CustomFieldRender;

@FieldRenderName("jdPricesFieldRender")
public class JdPricesFieldRender implements CustomFieldRender {

    @Override
    public void render(HttpRequest request, HttpResponse response, BeanMap beanMap, SpiderBean bean, Field field) {
        ProductList jd = (ProductList)bean;
        StringBuffer sb = new StringBuffer();
		/*for(String code : jd.getCodes()) {
			sb.append("J_").append(code).append(",");
		}*/
        String skuIds = sb.toString();
        try {
            skuIds = URLEncoder.encode(skuIds, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String url = "http://p.3.cn/prices/mgets?skuIds="+skuIds;
        HttpRequest subRequest = request.subRequest(url);
        try {
            HttpResponse subReponse = DownloaderContext.download(subRequest);
            String json = subReponse.getContent();
            List<JDPrice> prices = JSON.parseArray(json, JDPrice.class);
            beanMap.put(field.getName(), prices);
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}
